//package edu.washington.cs.workflow.analysis.guistructure;
//
//import org.uispec4j.Panel;
//import org.uispec4j.Trigger;
//import org.uispec4j.UIComponent;
//import org.uispec4j.Window;
//import org.uispec4j.finder.ComponentMatcher;
//import org.uispec4j.interception.WindowHandler;
//import org.uispec4j.interception.WindowInterceptor;
//
//import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent;
//import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEventFactory;
//import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEventOutcome;
//import edu.washington.cs.workflow.util.Globals;
//import edu.washington.cs.workflow.util.Utils;
//
///**
// * Randomly clicking the widgets on the panel
// * */
//public class MonkeyTester {
//
//	public final Panel panel;
//	
//	private int maxsize = 20;
//	
//	public MonkeyTester(Panel p) {
//		Utils.checkNotNull(p);
//		this.panel = p;
//	}
//		
//	public void setMaxsize(int size) {
//		Utils.checkTrue(maxsize > 0, "Wrong size value: " + maxsize);
//		this.maxsize = size;
//	}
//	
//	public EventSequence doTesting() {
//		EventSequence newSeq = new EventSequence();
//		newSeq.setOutcome(new NormalExecution());
//		
//		UIComponent[] comps = panel.getUIComponents(ComponentMatcher.ALL);
//		Object[] nonNullObjs = Utils.removeNulls(comps);
//		UIComponent[] uis = new UIComponent[nonNullObjs.length];
//		for(int i = 0; i < nonNullObjs.length; i++) {
//			uis[i] = (UIComponent)nonNullObjs[i];
//		}
//
//		for (int i = 0; i < this.maxsize; i++) {
//			UIComponent ui = uis[Utils.nextRandomInt(uis.length)];
//			UIEvent event = UIEventFactory.createEvent(ui, this.panel); 
////					EventFirer.fireEvent(ui);
////				System.out.println("mainWindow enable? " + mainWindow.isVisible().isTrue());
////			System.out.println("fire event: " + ui.getClass() + ", " + ui.getLabel());
//				if (event != null) {
//					Utils.debugPrintln("before event panel visibility: " + this.panel.isVisible().isTrue());
//					UIEventOutcome outcome = event.fireEvent();
//					Utils.debugPrintln(" -  after event panel visibility: " + this.panel.isVisible().isTrue());
//					if(outcome != null) {
//						Utils.debugPrintln("* event: " + event.toString());
//						Utils.debugPrintln("    - outcome: " + outcome.toString());
//						if(outcome.isSuccess()) {
//							if(!outcome.hasNewWindow()) {
//								//no window
//								newSeq.addUIEvent(event);
//							} else {
//								if(outcome.isModalWindow()) {
//									newSeq.addUIEvent(event);
//									//get the trigger
//									Utils.checkTrue(outcome.hasTrigger());
//									Trigger t = outcome.getTrigger();
//									
////									Panel modalDialog = EventSequenceReplayer.getModalDialog(t);
//									
//									WindowInterceptor.init(t)
//									  .process(new WindowHandler() {
//									    public Trigger process(Window dialog) {
//									      
////									    	System.out.println("name of mainwindow: " + panel.getName());
////									    	System.out.println("name of dialog: " + dialog.getTitle());
////									    	//XXX Must record the parent - child relationship
//////									    	System.out.println("name of parent: " + dialog);
////									    	System.out.println("+++ recursion starts...");
////									     MonkeyTester tester = new MonkeyTester(dialog);
////									     
////									     tester.setMaxsize(3);
////									     tester.doTesting(); //recursive
////									     tester.doTesting(); //recursive
////									     tester.doTesting(); //recursive
////									     tester.doTesting(); //recursive
////									        System.out.println("+++ recursion ends...");
//									     
//									        //XXX a problem, would any button be fine
//									      return Trigger.DO_NOTHING;//dialog.getButton("Cancel").triggerClick();
//									    }
//									  }).run();
//									
//									  
//									Utils.debugPrintln(" >> get a modal window, break now: " + Globals.lineSep);
//									break; //XXX not implemented
//									
//								} else if (outcome.isNonModalWindow()) {
//									newSeq.addUIEvent(event);
//									System.err.println("get a non-modal window, need to iterate now");
//									throw new Error("not implemented for a non-modal window");
//								} else {
//									throw new Error();
//								}
//							}
//						} else {
//							Throwable e = outcome.getError();
//							e.printStackTrace(); //for debugging
//							newSeq.addUIEvent(event);
//							newSeq.setOutcome(new ExceptionalExecution(e));
//							//hit an error then exit
//							break;
//						}
//					}
//				}
//
//		}
//		return newSeq;
//	}
//}
